<template>
  <div>
    <header class="app-header">
      <span>滑动删除</span>
    </header>
    <!-- <section>考试</section> -->
     <div class="container">
      <ul style="padding: 0; margin: 0">
        <li
          class="list-item2"
          v-for="(item, index) in list"
          data-type="0"
          :key="index"
        >
          <div
            class="list-box"
            @touchstart.capture="touchStart"
            @touchend.capture="touchEnd"
            :data-index="index"
          >
            <div class="list-content">
              <p class="title">{{ item.title }}</p>
              <p class="time">{{ item.time }}</p>
            </div>
          </div>
          <div class="delete" @click="deleteItem" :data-index="index">删除</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      endX: 0,
      list: [
        {
          title: "你好1",
        },
        {
          title: "你好2",
        },
        {
          title: "你好3",
        },
        {
          title: "你好4",
        },
      ],
    };
  },
  methods: {
    // 滑动开始
    touchStart(e) {
      // 记录初始位置
      this.startX = e.touches[0].clientX;
    },
    // 滑动结束
    touchEnd(e) {
      let index = e.currentTarget.dataset.index;
      // 当前滑动的父级元素
      let parentElement = e.currentTarget.parentElement;
      // 记录结束位置
      this.endX = e.changedTouches[0].clientX;
      // 左滑
      if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
        this.restSlide(index);
        parentElement.dataset.type = 1;
      }
      // 右滑
      if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
        this.restSlide(index);
        parentElement.dataset.type = 0;
      }
      this.startX = 0;
      this.endX = 0;
    },
    // 复位滑动状态
    restSlide(index) {
      let listItems = document.querySelectorAll(".list-item2");
      // 复位
      for (let i = 0; i < listItems.length; i++) {
        if (index == i) {
          listItems[i].dataset.type = 1;
        } else {
          listItems[i].dataset.type = 0;
        }
      }
    },
    // 删除
    deleteItem(e) {
      // 当前索引
      let index = e.currentTarget.dataset.index;
      // 复位
      this.restSlide();
      // 删除
      this.list.splice(index, 1);
    },
  },
};
</script>

<style scoped>
.container {
  overflow: hidden;
}
.list-item2 {
  position: relative;
  height: 50px;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  list-style: none;
}
.list-item2[data-type="0"] {
  transform: translate3d(1px, 0, 0);
}
.list-item2[data-type="1"] {
  transform: translate3d(-50px, 0, 0);
}
.list-box {
  padding: 0.2rem;
  display: flex;
  align-items: center;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  justify-content: flex-end;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  font-size: 0;
}
.list-item2 .list-content {
  padding: 0.1rem 0 0.1rem 0.2rem;
  position: relative;
  flex: 1;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  overflow: hidden;
}
.list-item2 .title {
  display: block;
  color: #333;
  overflow: hidden;
  font-size: 15px;
  font-weight: bold;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list-item2 .tips {
  display: block;
  overflow: hidden;
  font-size: 12px;
  color: #999;
  line-height: 20px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list-item2 .time {
  display: block;
  font-size: 12px;
  position: absolute;
  right: 0;
  top: 0.1rem;
  color: #666;
}
.list-item2 .delete {
  width: 50px;
  height: 50px;
  background: #ff4949;
  font-size: 15px;
  color: #fff;
  text-align: center;
  line-height: 50px;
  position: absolute;
  top: 0;
  right: -50px;
}

.list-item2:nth-child(2n) {
  background-color: white;
}

.list-item2:nth-child(2n + 1) {
  background-color: #42b983;
}
</style>
